//
//  LXJWeiboViewModel.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/31.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

/// 微博的 ViewModel

class LXJWeiboViewModel: NSObject {
    
    /// 数据源数组懒加载
    var homeWeiboList: LXJHomeWeiboModel?{
        didSet{
            /// 获取 weibo 认证类型
            let verifiedType = homeWeiboList?.user?.verified_type
            verifiedTypeImageHandle(verifiedType: verifiedType!)
            
            /// 获取微博的会员等级
            let mbrank: Int? = homeWeiboList?.user?.mbrank
            mbrankImageHandle(mbrank: mbrank ?? 0)
            
            /// 发表时间
            //  let created_at = homeWeiboList?.created_at
            //  createdTimeTextHandle(created_at: created_at)
            
            /// 发表来源
            let resource = homeWeiboList?.source
            resourceTextHandle(resource: resource)
            
            /// 转发数
            let repostsCount = homeWeiboList?.reposts_count
            repostsCountStr = countHandle(count: repostsCount!, placeholder: "转发")
            
            /// 评论数
            let commentsCount = homeWeiboList?.comments_count
            commentsCountStr = countHandle(count: commentsCount!, placeholder: "评论")
            
            /// 点赞数Str
            let attitudesCount = homeWeiboList?.attitudes_count
            attitudesCountStr = countHandle(count: attitudesCount!, placeholder: "点赞")
            
            /// 转发微博内容的处理
            retweetedWeiboContentHandle(homeWeiboList: homeWeiboList)
        }
    }
    
    /// 认证类型图片处理
    var verifiedTypeImage: UIImage?
    /// 认证类型 -1：没有认证，0,1，认证用户，2,3,5: 企业认证，220: 达人
    fileprivate func verifiedTypeImageHandle(verifiedType: Int) -> Void {
        switch verifiedType {
        case 0,1:
            verifiedTypeImage = UIImage(named: "avatar_vip")
            
        case 2,3,5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
            
        default:
            verifiedTypeImage = nil
        }
    }
    
    /// 会员等级图片处理
    /// 1-6个级别
    var mbrankImage: UIImage?
    fileprivate func mbrankImageHandle(mbrank: Int?) -> Void {
        if mbrank! >= 1 && mbrank! <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank!)")
        }else{
            mbrankImage = nil
        }
    }
    
    /// 发表时间处理 只读计算属性
    var createdTimeText: String?{
        /// 判断时间属性是否有值
        guard let createdTime = homeWeiboList?.created_at else { return nil}
        return createdTimeTextHandle(createdTime: createdTime)
    }
    /// 返回日期格式
    fileprivate func createdTimeTextHandle(createdTime: Date?) -> String {
        /// 获取日期格式对象
        let formatter = DateFormatter()
        /// 判断是否是今年
        if isThisYear(creatDate: createdTime) {
            /// 获取当前日历
            let calendar = Calendar.current
            
            /// 判断是否是今天
            if calendar.isDateInToday(createdTime!) {
                /// 获取发表至今的时间间隔
                let timeInterval = abs((createdTime?.timeIntervalSinceNow) ?? 0)
                
                /// 是否一分钟之内
                if timeInterval < 60 {
                    formatter.dateFormat = "刚刚"
                    /// 是否在一小时之内
                } else if timeInterval < 3600{
                    formatter.dateFormat = "\(Int(timeInterval / 60))分钟前"
                    /// 其他
                }else{
                    formatter.dateFormat = "\(Int(timeInterval / 3600))小时前"
                }
            }
                /// 判断是否是昨天
            else if calendar.isDateInYesterday(createdTime!){
                formatter.dateFormat = "昨天 HH:mm"
            }
                /// 其他
            else{
                formatter.dateFormat = "去年 MM-dd"
            }
            return formatter.string(from: createdTime!)
        }else{/// 不是今年
            /// 获取日期格式对象
            let formatter = DateFormatter()
            /// 设置日期格式
            formatter.dateFormat = "yyyy-MM-dd HH-mm"
            return formatter.string(from: createdTime!)
        }
    }
    /// 判断是否是今年
    fileprivate func isThisYear(creatDate: Date?) -> Bool{
        /// 获取日期格式对象
        let formatter = DateFormatter()
        /// 设置日期格式
        formatter.dateFormat = "yyyy"
        /// 获取传入日期的年份
        let creatYear = formatter.string(from: creatDate!)
        
        /// 获取当前日期的年份
        let currentYear = formatter.string(from: Date())
        
        /// 判断发表时间和今天时间比对结果
        return creatYear == currentYear
    }
    
    /// 发表来源处理"<a href="http://app.weibo.com/t/feed/4iDt6Q" rel="nofollow">微博网页</a>",
    var resourceText: String?
    fileprivate func resourceTextHandle(resource: String?) -> Void {
        /// 获取开始的截范围和结束的截取范围
        if let strartRange = resource?.range(of: "\">"), let endRange = resource?.range(of: "</") {
            /// 获取左面的最大光标
            let strartIndex = strartRange.upperBound
            
            /// 获取右面的最小
            let endIndex = endRange.lowerBound
            
            /// 截取目标字符串
            let source = resource?.substring(with: strartIndex..<endIndex)
            
            /// 来源赋值
            resourceText = "来自: " + source!
        }
    }
    
    /// 数量的处理
    /// 数量的字符串
    var repostsCountStr: String?
    var commentsCountStr: String?
    var attitudesCountStr: String?
    fileprivate func countHandle(count: Int, placeholder: String) -> String {
        if count == 0 {
            return placeholder
        } else if count > 9999{
            return "\(count / 1000)k"
        }else{
            return "\(count)"
        }
    }
    
    /// 转发微博内容的处理
    var retweetedWeiboContent: String?
    fileprivate func retweetedWeiboContentHandle(homeWeiboList: LXJHomeWeiboModel?) -> Void {
        let retweetedUserName = homeWeiboList?.retweeted_status?.user?.screen_name
        let retweetedWeiboText = homeWeiboList?.retweeted_status?.text
        retweetedWeiboContent = "@\(retweetedUserName ?? ""):\(retweetedWeiboText ?? "")"
    }
}
